package com.etoak.rst.sys.setmeal.controller;


import com.etoak.rst.sys.dish.DTO.DishDTO;
import com.etoak.rst.sys.setmeal.DTO.SetmealDTO;
import com.etoak.rst.sys.setmeal.entity.Setmeal;
import com.etoak.rst.sys.setmeal.service.SetmealService;
import com.etoak.rst.sys.util.JsonResponse;
import com.etoak.rst.sys.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService service;

    /**
     * 分页 +  模糊查询
     */
    @RequestMapping(value="/querySome",method = {RequestMethod.GET})
    public JsonResponse querySome(SetmealDTO dto){
        List<Setmeal> data = service.querySome(dto);
        PageInfo<Setmeal> info = new PageInfo<>(data);
        return data != null && data.size() > 0 ?
                        Result.success("200","success",info):
                        Result.failed("5000");
    }


    // 处理器解析器调用的是 HttpRequestHandlerMapping
    //有一个Map集合 key = uri value = handler 对象(处理器的信息)

    /**
     * 删除一堆套餐
     * @param ids
     * @return
     */
    @PostMapping(value = "deleteIds")
    public JsonResponse deleteIds(@RequestBody List<Integer> ids){
        try {
            service.deleteIds(ids);
            return Result.success("200","success","删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("50001");
        }

    }

    @PostMapping(value = "/addSetmeal")
    public JsonResponse addSetmeal(@RequestBody SetmealDTO setmealDTO){
        service.addSetMealAndDish(setmealDTO);

        return Result.success("添加成功");
    }


    @PutMapping("/{mealId}")
    public JsonResponse updateMealStatus(@PathVariable BigInteger mealId,@RequestBody Setmeal setmeal){
        service.updateMealStatus(mealId,setmeal);
        return Result.success("修改成功");
    }

    @PutMapping
    public JsonResponse updateMeal(@Validated @RequestBody SetmealDTO setmealDTO){
        service.updateMeal(setmealDTO);
        return Result.success("修改成功");
    }
}
